/**
 * Created by dongdh on 2016/8/10.
 */

/*
var timeoutTips = "您的二维码已过期，请点击<a id='refreshBtn' href='javascript:refreshCode()' class='red'>刷新</a>获取二维码"
var timeoutInitTips = '距离二维码过期还剩<span class="red" id="count"></span>秒，过期后请重新获取二维码';
var expiredInitTips = '此订单已过期，请重新下单支付!';
*/

var checkOrderTimes = 20 * 10;  //每分钟20次
var checkOrderResult = 3 ; //默认进行中

$(function(){
    // callTimes(initCount);

    checkOrderStatusTimes();

});

//二维码 刷新定时器
/*function times(){
    initCount--;
    $("#count").html(initCount);
    t = setTimeout(times, 1000);
    if ( initCount <= 0 ){
        clearTimeout(t);
        $('#scanPayTips').html(timeoutTips);
    }
}*/


function checkOrderStatusTimes(){
    checkOrderTimes--;

    if (checkOrderResult == 1 || checkOrderResult == 2 ) {
        //成功 或 失败
        clearTimeout(ost);

        window.location.href = ctx + "/scan/result.shtml?orderId=" + $("#orderId").val() + "&orderKey=" + $("#orderKey").val();

    } else if (checkOrderResult == 3) {
        //进行中
        getOrderStatus();
    }
    ost = setTimeout(checkOrderStatusTimes, 3000);
    if ( checkOrderTimes <= 0 ){
        clearTimeout(ost);
    }
}


/**
 * 获取订单状态
 */
function getOrderStatus() {

    $.ajax({
        url:ctx+"/pay/getOrderResult.shtml?"+(new Date()).getTime(),
        async:false,
        type:"GET",
        dataType:"JSON",
        data:{orderId: $("#orderId").val()}
    }).done(function(data){
        checkOrderResult = data.result;
    });
}

/*
function callTimes(count) {
    $('#scanPayTips').html(timeoutInitTips);
    initCount = count;
    times();
}*/

/**
 * 刷新 二维码
 */
/*
function refreshCode() {

    var orderId = $("#orderId").val();
    var orderKey = $('#orderKey').val();
    var bankCode = $("#bankCode").val();
    //alert("刷新二维码orderId=" + orderId);
    var url = ctx + "/scan/refreshCode.shtml";
    updateRefreshBtn(false);
    $.ajax({
        type: 'POST',
        url: url ,
        data: {"orderId":orderId, "orderKey": orderKey, "bankCode" : bankCode} ,
        success: function(data){
            if (data.result == 1){
            	var imgurl = data.url;
            	if(imgurl.indexOf("weixin://wxpay/") != 0){
            	  var start = imgurl.indexOf("weixin://wxpay/");
            	  imgurl = imgurl.substring(start)
            	}
                refeshQrcode(imgurl);
                callTimes(data.timeout);
            } else {
                if (data.result == 3){
                    $('#scanPayTips').html(expiredInitTips);
                }else{
                    refeshQrcode($("#output").attr("src"));
                    callTimes(data.timeout);//add by zhaojt 服务端异常时，假刷新。不提示错误信息。
                }
            }
            updateRefreshBtn(true);
        } ,
        error: function() {
            alert('服务器异常，请稍后重试');
            updateRefreshBtn(true);
        },
        dataType: "json"
    });
}

//处理刷新按钮
function updateRefreshBtn(enable) {
    if (enable) {
        $('#refreshBtn').attr("href", "javascript:refreshCode()");
    } else if (!enable) {
        $('#refreshBtn').removeAttr("href");
    }
}

//刷新二维码
function refeshQrcode(url){
    //把之前的二维码元素删掉
    $('#output').find("canvas").remove();
    //重新生成
    window.setTimeout(function(){
        jQuery('#output').qrcode({width:191,height:191, text:url});
    },100);
    //记录此次生成的url
    $('#output').attr('src', url);
}*/
